package c.x.jy.service.config.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import c.x.jy.common.annotation.DelCache;
import c.x.jy.dao.dao.BusinessConfigDao;
import c.x.jy.dao.entity.BusinessConfigDefaultEntity;
import c.x.jy.dao.entity.BusinessConfigEntity;
import c.x.jy.service.config.BusinessConfigService;
import c.x.jy.service.entity.form.CommisionConfig;
import c.x.jy.service.utils.SpringContextUtils;

@CacheConfig(cacheNames = { "Business" })
@Service("businessConfigService")
public class BusinessConfigServiceImpl extends ServiceImpl<BusinessConfigDao, BusinessConfigEntity> implements BusinessConfigService {
	@Autowired
private	BusinessConfigDao businessConfigDao;
	
 

	@Override
	public List<BusinessConfigDefaultEntity> queryList(Long businessId) {
		// TODO Auto-generated method stub
		if(businessId>0) {
			return businessConfigDao.queryList(  businessId);	
		}else {
			return businessConfigDao.queryDelultList(  );
		}
	
	}
	//@CacheEvict(key="'Business:getBusinessConfigValue:'+#p1+':'+*" )
	//Business:BusinessConfig:
	@DelCache({"Business","BusinessConfig"})
    @Transactional
	@Override
	public void updateConfig(Map<String, Object> params, Long businessId) {
     
    	QueryWrapper<BusinessConfigEntity> wrapper=new QueryWrapper<>();
		List<Object> keyParam = new ArrayList<>();
	 	wrapper.eq("business_id", businessId);
		params.forEach((k,v)-> keyParam.add(k));
		wrapper.in("ckey", keyParam);
		businessConfigDao.delete(wrapper);
		List<BusinessConfigEntity> list=new ArrayList<>();
		params.forEach((k,v)->{ 
			BusinessConfigEntity be=new BusinessConfigEntity();
			be.setBusinessId(businessId);
			be.setConfigKey(k);
			be.setConfigValue(v!=null?v.toString():null);
			list.add(be);

		});
		this.baseMapper.insertbatths  (list);
	}
	@Override
    @Cacheable(key = "'BusinessConfig:'+ methodName +':' +#p0+':'+#p1",unless="#result == null")
	public String getBusinessConfigValue(Long businessId, String key,String devutlevalue) {
    	if(businessId!=null&&!businessId.equals(0l)) {
    	BusinessConfigEntity bconfig=	businessConfigDao.selectByBusinessIdAndKey(businessId,key);
    	if(bconfig!=null)  	return bconfig.getConfigValue();
     
    	}
    	BusinessConfigDefaultEntity bcdesc=	businessConfigDao.selectDefultByBusinessIdAndKey(  key);    	
		return bcdesc==null?devutlevalue: bcdesc.getDefaultValue();
		
	}

	@Override
	public String getBusinessConfigValue(Long businessId, String key) {
		return SpringContextUtils.getBean(BusinessConfigServiceImpl.class).getBusinessConfigValue(businessId, key, null);
		
	}
    
	@Override
	public CommisionConfig selsetCommisionConfig() {
		CommisionConfig m=new CommisionConfig();
		m.setAgentslef(SpringContextUtils.getBean(BusinessConfigServiceImpl.class).getBusinessConfigValue(null, "AGENT_ORDER_COMMISION_AGENT","0"));
		m.setMemberReferences(SpringContextUtils.getBean(BusinessConfigServiceImpl.class).getBusinessConfigValue(null, "AGENT_ORDER_COMMISION_MEMBER_REFERENCES","0"));
		m.setMemberslef(SpringContextUtils.getBean(BusinessConfigServiceImpl.class).getBusinessConfigValue(null, "AGENT_ORDER_COMMISION_MEMBER_SELF","0"));
		m.setTeacherReferences(SpringContextUtils.getBean(BusinessConfigServiceImpl.class).getBusinessConfigValue(null, "AGENT_ORDER_COMMISION_TEACHER_REFERENCES","0"));
		m.setTeacherslef(SpringContextUtils.getBean(BusinessConfigServiceImpl.class).getBusinessConfigValue(null, "AGENT_ORDER_COMMISION_TEACHESELF","0"));
		return m;
	}

}
